/*-------------------------------------------------------------------------
 - Copyright (c) 2024-2025 [XD-AMCC TEAM]
 - [XD-AMCC] is licensed under Mulan PSL v2.
 - You can use this software according to the terms and conditions of the Mulan PSL v2.
 - You may obtain a copy of Mulan PSL v2 at:
 -          http://license.coscl.org.cn/MulanPSL2
 - THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES
 - OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
 - TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
 - See the Mulan PSL v2 for more details.
 ------------------------------------------------------------------------*/


/**
 * @brief  Recursive-Descent-Function of non-terminal primaryExpression.
 *
 * @author  WXQ#XDU
 * @date    2024.12
 */

#include "../_parser_inner.h"

#define THIS_NT_ID  primaryExpression

// primaryExpression           //------------------ LL(2) production 87..91
//    :  INT_LITERAL
//    |  REAL_LITERAL
//    |  '(' expression ')'
//    |  functionCall   // LA(1) is ID, and LA(2) is '('
//    |  leftValue      // LA(1) is ID, and LA(2) is not '('
//
t_tree_ptr RDF_primaryExpression (t_parser_ptr pThis) {
    int productionId = PRD_ID_UNKNOWN;
    t_tree_ptr root = tree_new_parent(THIS_NT, productionId);
    enter_nonterminal ( pThis, THIS_NT, root );
    CHECK_NULLTREE_ABORT ( root, root );
// -------  BEGIN of local action

    t_tree_ptr  subTree = NULL;
    switch( LOOK_AHEAD(pThis) ) {
        case TK_INT_LITERAL :       productionId = 87;
            subTree = parser_match(pThis, TK_INT_LITERAL );
            CHECK_NULLTREE_ABORT ( subTree, root );
            tree_add_child(root, subTree);
            break;
        case TK_REAL_LITERAL :    productionId = 88;
            subTree = parser_match(pThis, TK_REAL_LITERAL );
            CHECK_NULLTREE_ABORT ( subTree, root );
            tree_add_child(root, subTree);
            break;
        case TK_LPAREN :            productionId = 89;  // '(' expression ')'
            subTree = parser_match(pThis, TK_LPAREN );   // consume '('
            CHECK_NULLTREE_ABORT ( subTree, root );
            tree_add_child(root, subTree);

            subTree = RDF_expression(pThis);             // analyze expr
            CHECK_NULLTREE_ABORT ( subTree, root );
            tree_add_child(root, subTree);

            subTree = parser_match(pThis, TK_RPAREN );   // consume ')'
            CHECK_NULLTREE_ABORT ( subTree, root );
            tree_add_child(root, subTree);
            break;
        case TK_ID:
            if (TK_LPAREN == LOOK_AHEAD_K(pThis, 2)) { // ID(...), a func-call
                productionId = 90;
                subTree = RDF_functionCall(pThis);
                CHECK_NULLTREE_ABORT ( subTree, root );
                tree_add_child(root, subTree);
            } else {                             // ID or ID[...][...]...
                productionId = 91;
                subTree = RDF_leftValue(pThis);
                CHECK_NULLTREE_ABORT ( subTree, root );
                tree_add_child(root, subTree);
            }
            break;
        default:
            parser_report_badtoken(pThis, NEXT_TOKEN(pThis),
                                   TK_ERROR, "primaryExpression");
            tree_destroy( root );  root = NULL;
            goto LABEL_EXIT_RDF;
    }


// -------  END of local action
    if (PRD_ID_UNKNOWN == productionId) {
        ERROR_REPORT_BUG("Forgot to correct the value of "
                         "productionId in RDF_" NT_TEXT "()");
    }
    tree_reset_production( root, productionId );

LABEL_EXIT_RDF:
    exit_nonterminal  ( pThis, THIS_NT, root );
    return root;
}

